<template>
    <div 
        v-wechat-title="$route.meta.title"
    >
        登录中
    </div>
</template>

<script>
import axios from 'axios'
import { mapMutations } from 'vuex'
import { sep } from 'path';
export default {
    name: 'Author',
    created () {
        //测试阶段登录
        this.login()
        //1.判断是不是微信浏览器
        // let ua = window.navigator.userAgent.toLowerCase()
        
        // if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        //     // 2.判断是否存在本地数据
        //     if(!this.common.getCookie('openid')) {  
        //         //跳转到微信授权页面
        //         window.location.href = 'http://wxx.qipigou.com'+'/api/login/isLogin'
        //     }else{
        //         //判断有没有用户信息 
        //         this.userInfo = this.common.getUserInfo()
                
        //         if (this.userInfo != null) {
        //             console.log(this.loginRefresh())
        //             // 如果有cookie 再判断用户cookie是不是快过期 判断cookie是否快过期再userInfo的lastLoginTime判断
        //             if(this.loginRefresh()) {
                        
        //                 //重新登录
        //                 this.login()
        //             }else{
        //                 //跳转到登录前的页面
        //                 this.goBeforeLoginUrl()
        //             }
        //         }else{
        //             //初次登录或者用户删除数据
        //             this.login()
        //         }
                
        //     }
        // }else{
        //     //需要微信网页才能打开
        // }
    },
    data () {
        return {
            userInfo: null
        }
    },
    methods: {
        ...mapMutations([
            'user'
        ]),
        login () {
            //测试阶段登录
            let data = {
                subscribe:1,
                openid:'oXZV00usRzJ2o6r45j3ApCkOVrok',
                nickname:"Rong@Sheng",
                sex:1,
                language:"zh_CN",
                city:"江门",
                province:"广东",
                country:"中国",
                headimgurl:"http://thirdwx.qlogo.cn/mmopen/ZiaUBz1LfJQEvAz5aibovgibggsDB6vrRsJeic5pS4ILv5L7eImiauLeIrCibSDVesj6tWvFkO9fVkibcOkfnhFric9HnXGPtaKdkIl8/132",
                subscribe_time:1534472603,
                remark:"",
                groupid:0,
                tagid_list:[],
                subscribe_scene:"ADD_SCENE_SEARCH",
                qr_scene:0,
                qr_scene_str:"",
                lastLoginTime: new Date().getTime(),
                userid:"100000"
            }
            this.common.setUserInfo(data)

            this.goBeforeLoginUrl()

            // let url = 'http://wxx.qipigou.com/api/login/getUserInfo'
            // let _this = this
            // axios.get(url)
            //     .then(function (response) {
            //         const data = response.data
            //         _this.common.setUserInfo(data)
            //         setTimeout(() => {
            //             _this.goBeforeLoginUrl()
            //         },2000)
            //     })
            //     .catch(function (error) {
            //         console.log(error);
            //     });
        },
        goBeforeLoginUrl () {
            let url = this.common.getCookie('beforeLoginUrl')
            if ( !url || url.indexOf('/author') != -1) {
                this.$router.push('/')
            } else {
                if( url == '/' ) {
                    url = '/'
                }
                this.common.setCookie('beforeLoginUrl', '')
                this.$router.push(url)
            }
        },
        loginRefresh () {
            //判断用户是否快要过期 快要过期就续期 否则继续 去后台获取 
            const userInfo = this.userInfo
            let nowtime = new Date().getTime()
            let Expiration = 60 * 60 * 2 * 1000
            if( nowtime - userInfo.lastLoginTime >= Expiration ) {
                return true
            } else {
                return false
            }
        }
    }
}
</script>

<style lang="stylus" scoped>

</style>

